
package com.sunwayhorizo.health.hl7.datatypes.common;

import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.common.VersionInfo;
import com.sunwayhorizo.health.hl7.datatypes.ANY;
import com.sunwayhorizo.health.hl7.datatypes.BL;
import com.sunwayhorizo.health.hl7.datatypes.CS;
import com.sunwayhorizo.health.hl7.datatypes.EN;
import com.sunwayhorizo.health.hl7.datatypes.ENXP;
import com.sunwayhorizo.health.hl7.datatypes.INT;
import com.sunwayhorizo.health.hl7.datatypes.IVL_TS;
import com.sunwayhorizo.health.hl7.datatypes.LIST_ENXP;
import com.sunwayhorizo.health.hl7.datatypes.SET_CS;
import com.sunwayhorizo.health.hl7.datatypes.ST;
import com.sunwayhorizo.health.hl7.datatypes.TN;
import com.sunwayhorizo.health.hl7.datatypes.TYPE;

import java.util.Iterator;


// Referenced classes of package oracle.apps.ctb.hl7.types.common:
//            ANYImpl, BLImpl, CSImpl, ENXPImpl, 
//            INTImpl, IVL_TSImpl, LIST_ENXPAdapterImpl, NullFlavorImpl, 
//            SET_CSImpl, TYPEImpl

public class TNImpl extends ANYImpl
    implements TN
{

    public static final String RCS_ID = "$Header: TNImpl.java 115.30 2005/09/16 09:23:27 amchanda noship $";
    public static final boolean RCS_ID_RECORDED = VersionInfo.recordClassVersion("$Header: TNImpl.java 115.30 2005/09/16 09:23:27 amchanda noship $", "com.sunwayhorizo.health.hl7.datatypes.common");
    private String _name;
    private static final TN nullNI = new TNImpl(CSImpl.valueOf("NI"));
    private static final TN nullOTH = new TNImpl(CSImpl.valueOf("OTH"));
    private static final TN nullNINF = new TNImpl(CSImpl.valueOf("NINF"));
    private static final TN nullPINF = new TNImpl(CSImpl.valueOf("PINF"));
    private static final TN nullUNK = new TNImpl(CSImpl.valueOf("UNK"));
    private static final TN nullASKU = new TNImpl(CSImpl.valueOf("ASKU"));
    private static final TN nullNAV = new TNImpl(CSImpl.valueOf("NAV"));
    private static final TN nullMSK = new TNImpl(CSImpl.valueOf("MSK"));
    private static final TN nullNA = new TNImpl(CSImpl.valueOf("NA"));
    private static final TN nullNASK = new TNImpl(CSImpl.valueOf("NASK"));

    public static TN valueOf(String s)
    {
        return new TNImpl(s);
    }

    public static TN nullTN()
    {
        return new TNImpl(((CS) (null)));
    }

    protected TNImpl(CS cs)
    {
        super(true, cs);
        _name = null;
    }

    protected TNImpl(String s)
    {
        if(s == null)
        {
            throw ANYImpl.newNullException("TN", "constructor", "name");
        } else
        {
            _name = s;
            return;
        }
    }

    public TYPE dataType()
    {
        return com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.TN;
    }

    public BL equal(ANY any)
    {
        if(!(any instanceof EN))
            return BLImpl.FALSE;
        EN en = (EN)any;
        if(isNull().or(any.isNull()).isTrue())
            return BLImpl.FALSE;
        else
            return head().equal(en.head()).and(en.tail().isEmpty());
    }

    public SET_CS use()
    {
        return SET_CSImpl.nullSET_CS(NullFlavorImpl.NA);
    }

    public IVL_TS validTime()
    {
        return IVL_TSImpl.nullIVL_TS(NullFlavorImpl.NA);
    }

    public ST formatted()
    {
        return head();
    }

    public ENXP head()
    {
        if(isNull().isTrue())
            return ENXPImpl.nullENXP(NullFlavorImpl.NI);
        else
            return ENXPImpl.valueOf(_name, CSImpl.nullCS(NullFlavorImpl.NI), SET_CSImpl.nullSET_CS(NullFlavorImpl.NI));
    }

    public LIST_ENXP tail()
    {
        if(isNull().isTrue())
            return LIST_ENXPAdapterImpl.nullLIST_ENXP(NullFlavorImpl.NI);
        else
            return LIST_ENXPAdapterImpl.valueOf(new ENXP[0]);
    }

    public ENXP item(INT int1)
    {
        if(isNull().isTrue() || int1.isZero().isFalse())
            return ENXPImpl.nullENXP(NullFlavorImpl.NI);
        else
            return head();
    }

    public BL contains(ENXP enxp)
    {
        if(isNull().isTrue())
            return BLImpl.nullBL(NullFlavorImpl.NI);
        else
            return head().equal(enxp);
    }

    public BL isEmpty()
    {
        if(isNull().isTrue())
            return BLImpl.nullBL();
        else
            return BLImpl.FALSE;
    }

    public BL notEmpty()
    {
        if(isNull().isTrue())
            return BLImpl.nullBL();
        else
            return BLImpl.TRUE;
    }

    public INT length()
    {
        if(isNull().isTrue())
            return INTImpl.nullINT();
        else
            return INTImpl.valueOf(1L);
    }
    
    public int hashCode()
    {
        return _name.hashCode();
    }

    public Iterator listIterator()
    {
        return new Iterator() {

            private boolean _beforeFirst;

            public boolean hasNext()
            {
                return _beforeFirst;
            }

            public Object next()
            {
                if(_beforeFirst)
                {
                    _beforeFirst = false;
                    return ENXPImpl.valueOf(_name, CSImpl.nullCS(NullFlavorImpl.NI), SET_CSImpl.nullSET_CS(NullFlavorImpl.NI));
                } else
                {
                    throw ANYImpl.newNullException("TN", "listIterator", "Iterator");
                }
            }

            public void remove()
            {
                throw ANYImpl.newUnsupportedException("TN", "remove");
            }

            
            {
                _beforeFirst = true;
            }
        };
    }

//    public XMLNode[] writeXML(XMLDocument xmldocument, String s)
//        throws Hl7Exception
//    {
//        try
//        {
//            XMLElement xmlelement = (XMLElement)xmldocument.createElement(s);
//            if(_name != null)
//            {
//                org.w3c.dom.Text text = xmldocument.createTextNode(_name);
//                xmlelement.appendChild(text);
//            }
//            return (new XMLNode[] {
//                xmlelement
//            });
//        }
//        catch(Exception exception)
//        {
//            throw new Hl7Exception(exception);
//        }
//    }

    public static TN nullTN(CS cs)
    {
        String s = null;
        if(cs == null || cs.literal().stringValue() == null || cs.isNull().isTrue())
            s = "NI";
        else
            s = cs.literal().stringValue();
        if(s.equals("NI"))
            return nullNI;
        if(s.equals("OTH"))
            return nullOTH;
        if(s.equals("NINF"))
            return nullNINF;
        if(s.equals("PINF"))
            return nullPINF;
        if(s.equals("UNK"))
            return nullUNK;
        if(s.equals("ASKU"))
            return nullASKU;
        if(s.equals("NAV"))
            return nullNAV;
        if(s.equals("MSK"))
            return nullMSK;
        if(s.equals("NA"))
            return nullNA;
        if(s.equals("NASK"))
            return nullNASK;
        else
            return nullNI;
    }


}
